package cn.edu.gcu.lost.util;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.web.multipart.MultipartFile;

public class UploadFile {

    public static String updatePic(String basePath, MultipartFile file) throws Exception {
        String result = null;
        String fileName = file.getOriginalFilename();
        String fileType = fileName.substring(fileName.lastIndexOf("."), fileName.length());
        String newFileName = System.currentTimeMillis() + fileType;
        String filePath = new SimpleDateFormat("yy/MM/dd").format(new Date()).toString();
        String path = basePath + "res/img/" + filePath;
        File newFile = new File(path);
        if (!newFile.isDirectory()) {
            newFile.mkdirs();
        }
        File resultFile = new File(path + "/" + newFileName);
        file.transferTo(resultFile);
        result = filePath + '/' + newFileName;
        return result;
    }
}
